package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.dao.SmsCouponDao;
import com.macro.mall.dao.SmsCouponProductCategoryRelationDao;
import com.macro.mall.dao.SmsCouponProductRelationDao;
import com.macro.mall.dto.SmsCouponParam;
import com.macro.mall.dto.UmsAdminDTO;
import com.macro.mall.mapper.SmsCouponMapper;
import com.macro.mall.mapper.SmsCouponProductCategoryRelationMapper;
import com.macro.mall.mapper.SmsCouponProductRelationMapper;
import com.macro.mall.mapper.UmsStoreMapper;
import com.macro.mall.model.*;
import com.macro.mall.service.SmsCouponService;
import com.macro.mall.service.UmsAdminService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 优惠券管理Service实现类
 * Created by macro on 2018/8/28.
 */
@Service
public class SmsCouponServiceImpl implements SmsCouponService {
    @Autowired
    private SmsCouponMapper couponMapper;
    @Autowired
    private SmsCouponProductRelationMapper productRelationMapper;
    @Autowired
    private SmsCouponProductCategoryRelationMapper productCategoryRelationMapper;
    @Autowired
    private SmsCouponProductRelationDao productRelationDao;
    @Autowired
    private SmsCouponProductCategoryRelationDao productCategoryRelationDao;
    @Autowired
    private SmsCouponDao couponDao;
    @Autowired
    private UmsAdminService umsAdminService;
    @Autowired
    private UmsStoreMapper umsStoreMapper;

    @Override
    public int create(String currentUserName, SmsCouponParam couponParam) {
        couponParam.setCount(couponParam.getPublishCount());
        couponParam.setUseCount(0);
        couponParam.setReceiveCount(0);
        UmsAdminDTO admin = this.umsAdminService.getAdminDTOByUserName(currentUserName);
        couponParam.setMemberId(admin.getId());
        couponParam.setMemberUserName(admin.getUserName());
        couponParam.setStoreId(admin.getStoreId());
        couponParam.setEnableTime(couponParam.getStartTime());
        //插入优惠券表
        int count = couponMapper.insert(couponParam);
        //插入优惠券和商品关系表
        if (couponParam.getUseType().equals(2)) {
            for (SmsCouponProductRelation productRelation : couponParam.getProductRelationList()) {
                productRelation.setCouponId(couponParam.getId());
                productRelation.setMemberId(admin.getId());
                productRelation.setMemberUserName(admin.getUserName());
                productRelation.setStoreId(admin.getStoreId());
            }
            productRelationDao.insertList(couponParam.getProductRelationList());
        }
        //插入优惠券和商品分类关系表
        if (couponParam.getUseType().equals(1)) {
            for (SmsCouponProductCategoryRelation couponProductCategoryRelation : couponParam.getProductCategoryRelationList()) {
                couponProductCategoryRelation.setCouponId(couponParam.getId());
                couponProductCategoryRelation.setMemberId(admin.getId());
                couponProductCategoryRelation.setMemberUserName(admin.getUserName());
                couponProductCategoryRelation.setStoreId(admin.getStoreId());
            }
            productCategoryRelationDao.insertList(couponParam.getProductCategoryRelationList());
        }
        return count;
    }

    @Override
    public int delete(Long id) {
        //删除优惠券
        int count = couponMapper.deleteByPrimaryKey(id);
        //删除商品关联
        deleteProductRelation(id);
        //删除商品分类关联
        deleteProductCategoryRelation(id);
        return count;
    }

    private void deleteProductCategoryRelation(Long id) {
        SmsCouponProductCategoryRelationExample productCategoryRelationExample = new SmsCouponProductCategoryRelationExample();
        productCategoryRelationExample.createCriteria().andCouponIdEqualTo(id);
        productCategoryRelationMapper.deleteByExample(productCategoryRelationExample);
    }

    private void deleteProductRelation(Long id) {
        SmsCouponProductRelationExample productRelationExample = new SmsCouponProductRelationExample();
        productRelationExample.createCriteria().andCouponIdEqualTo(id);
        productRelationMapper.deleteByExample(productRelationExample);
    }

    @Override
    public int update(String currentUserName, Long id, SmsCouponParam couponParam) {
        UmsAdminDTO admin = this.umsAdminService.getAdminDTOByUserName(currentUserName);
        if (admin.getId().equals(couponParam.getMemberId())) {
            couponParam.setId(id);
            //修改优惠券的数量
            if (couponParam.getPublishCount() != null && couponParam.getReceiveCount() != null) {
                couponParam.setCount(couponParam.getPublishCount() - couponParam.getReceiveCount());
            }
            couponParam.setEnableTime(couponParam.getStartTime());
            int count = couponMapper.updateByPrimaryKey(couponParam);
            //删除后插入优惠券和商品关系表
            if (couponParam.getUseType().equals(2)) {
                for (SmsCouponProductRelation productRelation : couponParam.getProductRelationList()) {
                    productRelation.setCouponId(couponParam.getId());
                }
                deleteProductRelation(id);
                productRelationDao.insertList(couponParam.getProductRelationList());
            }
            //删除后插入优惠券和商品分类关系表
            if (couponParam.getUseType().equals(1)) {
                for (SmsCouponProductCategoryRelation couponProductCategoryRelation : couponParam.getProductCategoryRelationList()) {
                    couponProductCategoryRelation.setCouponId(couponParam.getId());
                }
                deleteProductCategoryRelation(id);
                productCategoryRelationDao.insertList(couponParam.getProductCategoryRelationList());
            }
            return count;
        } else {
            return -2;
        }
    }

    @Override
    public List<SmsCoupon> list(UmsAdminDTO umsAdminDTO,String name, Integer type, Integer pageSize, Integer pageNum) {

        SmsCouponExample example = new SmsCouponExample();
        SmsCouponExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (type != null) {
            criteria.andTypeEqualTo(type);
        }
        if(umsAdminDTO.getAdminType().equals(2)){
            criteria.andStoreIdEqualTo(umsAdminDTO.getStoreId());
        }
        if(umsAdminDTO.getAdminType().equals(1)){
            //省级管理员，可以看到他下面所有的用户的商品列表
            UmsStoreExample storeExample = new UmsStoreExample();
            storeExample.createCriteria().andParentIdEqualTo(umsAdminDTO.getId());
            List<Long> stoIds = this.umsStoreMapper.selectByExample(storeExample).stream().map(UmsStore::getId).collect(Collectors.toList());
            criteria.andStoreIdIn(stoIds);
        }
        PageHelper.startPage(pageNum, pageSize);
        example.setOrderByClause("id desc");
        return couponMapper.selectByExample(example);
    }

    @Override
    public SmsCouponParam getItem(Long id) {
        return couponDao.getItem(id);
    }
}
